package com.etc.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.entity.Doctor;
import com.etc.entity.Duty;
import com.etc.entity.Medicine;
import com.etc.entity.Outpatientservicetype;
import com.etc.service.DutyService;
import com.etc.service.OutpatientservicetypeService;

@Controller
public class DutyController {
	@Resource
	private DutyService dutyService;
	
	/**
	 * 查询所有科室
	 * @return
	 */
	@RequestMapping(value="duty")
	public String selectDuty(Model model,HttpServletRequest request) {	
		List<Duty> duty = dutyService.selectDuty();
		model.addAttribute("list", duty);
		request.getSession().setAttribute("duty",duty );
		
		return "forward:/back/wook-management.jsp";
	}
	
	/**
	 * 添加值班到后台页面
	 * @param 
	 * @return
	 */
	@RequestMapping(value="addDuty",method=RequestMethod.POST)
	@ResponseBody
	public int addDuty(@RequestBody Duty duty) {
		int i = dutyService.insertDuty(duty);
		return i;
		
	}
	
	/**
	 * 查询所有值班 传到后台页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "dutyListBack", method = RequestMethod.GET)
	@ResponseBody
	public String getDoctorBack(HttpServletRequest request) {
		List<Duty> dutyListBack= dutyService.selectDuty();
		//把所有医生对象放在session里，方便其他页面用
		request.getSession().setAttribute("dutyListBack", dutyListBack);
		return "forward:/back/wook-management.jsp";
	}
	
	/**
	 * 后台修改值班信息
	 * @param duty
	 * @return
	 */
	@RequestMapping(value="dutyUpd/{dutyid}",method=RequestMethod.PUT)
	@ResponseBody
	public int updDuty(@PathVariable(value="dutyid") int dutyid,@RequestBody Duty duty) {
		int i = dutyService.updDuty(duty);
		return i;
	}
	
	/**
	 * 分页操作
	 * @param duty
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="dutyPage/{page}/{pageSize}",method=RequestMethod.GET)
	@ResponseBody
	public List<Duty> page(Model model,@PathVariable(value="page")int page,@PathVariable(value="pageSize")int pageSize) {		
		int startPage=(page-1)*pageSize;//从第几页开始显示数据
		List<Duty> dutyList = dutyService.selectDutyByPage(startPage, pageSize);
		int count=dutyService.selCountDuty();
		model.addAttribute("duty",dutyList);
		model.addAttribute("count",count);
		model.addAttribute("page",page);
		
		return dutyList;
	}
}
